<template>
	<view class="page">
		<view class="searchBox">
			<view class="inputBox">
				<view class="inputRow" style="" @click="timeRangeShow=true" >
					<view class="label">
						{{language.bedtimeStatistics.type}}:
					</view>
					<view :class="params.timeRange ? 'val' : 'emty'" >
						{{timeRangeStr  ? timeRangeStr  : language.bedtimeStatistics.tip }}
					</view>
				</view>
				<view class="inputRow" style="" @click="attDateShow=true" >
					<view class="label">
						{{language.bedtimeStatistics.date}}:
					</view>
					<view :class="params.attDate ? 'val' : 'emty'" >
						{{params.attDate  ? params.attDate  : language.bedtimeStatistics.tip }}
					</view>
				</view>
			</view>
			<view class="btnBox">
				<view class="btn"  @click="goSearch">
					{{language.bedtimeStatistics.search}}
				</view>
			</view>
		</view>
		<!-- <view class="totalRow">
			<view class="item">
				<view class="label">
					在校人数
				</view>
				<view class="num">
					{{totalObj.inschool ? otalObj.inschool : 0}}
				</view>
			</view>
			<view class="item">
				<view class="label">
					离校人数
				</view>
				<view class="num">
					{{totalObj.outschool ? otalObj.outschool : 0}}
				</view>
			</view>
			
		</view> -->
		<u-subsection  :list="selectList" :current="current"  @change="sectionChange"></u-subsection>
		
			<scrollYBox styleText="height: calc( 100vh - 220rpx );" @scrolltolower="scrolltolower" :list="list">
						<template>
							<view style="padding-bottom: 50rpx;">
								<view class="boxItem" v-for="(item,index) in list" :key="index">
									<view class="row flex">
										<view class="item ">
											<view class="label">
												{{selectList[current]}}
											</view>
											<view class="val">
												{{item.showName  }}
											</view>
										</view>
										<view class="item ">
											<view class="label">
												{{language.bedtimeStatistics.label1}}
											</view>
											<view class="val">
												{{item.studentcount | emptyNum  }}
											</view>
										</view>
										
										
									</view>
									<view class="row flex">
										<view class="item ">
											<view class="label">
												{{language.bedtimeStatistics.label2}}
											</view>
											<view class="val">
												{{item.livecount | emptyNum }}
											</view>
										</view>
										<view class="item ">
											<view class="label">
												{{language.bedtimeStatistics.label3}}
											</view>
											<view class="val">
												{{item.zdcount | emptyNum }}
											</view>
										</view>
										
										
									</view>
									<view class="row flex">
										<view class="item ">
											<view class="label">
												{{language.bedtimeStatistics.label4}}
											</view>
											<view class="val">
												{{item.entercount | emptyNum }}
											</view>
										</view>
										<view class="item ">
											<view class="label">
												{{language.bedtimeStatistics.label5}}
											</view>
											<view class="val">
												{{item.unentercount | emptyNum }}
											</view>
										</view>
										<view class="item ">
											<view class="label">
												{{language.bedtimeStatistics.label6}}
											</view>
											<view class="val">
												{{item.leavecount | emptyNum  }}
											</view>
										</view>
									</view>
								</view>
							</view>
						</template>
					</scrollYBox>
	
		<u-datetime-picker
		    :show="attDateShow" ref="datetimePicker" v-model="attDate" 
		    mode="date"  @cancel="attDateShow=false" @confirm="attDateConfirm"
		       ></u-datetime-picker>
		<u-datetime-picker
			:show="accessDate_endShow" ref="datetimePicker" v-model="accessDate_end" 
			mode="date"  @cancel="accessDate_endShow=false" @confirm="accessDate_endConfirm"
	       ></u-datetime-picker>
		<u-picker :show="timeRangeShow" ref="uPicker" :columns="timeRangeColumns" @cancel="timeRangeShow=false" keyName="label"
		  @confirm="timeRangeConfirm"></u-picker>
	</view>
</template>

<script>
	
	import {personStatisticsApi} from '@/common/api/commonApi/dormApi.js'
	import {queryAccessByTypeApi,getDepartBypidOridApi} from '@/common/api/commonApi/safeApi.js'
	export default {
		data() {
			return {
				list:[],
				params:{
					timeRange: '',
					attDate :'',
					// pageNo: 1,
					// pageSize: 10
				},
				timeRangeStr: '',
				attDate: Number(new Date()),
				accessDate_end: Number(new Date()),
				attDateShow : false,
				accessDate_endShow : false,
				total : 0,
				isTeacher: uni.getStorageSync('isTeacher'),
				total: '',
				totalObj:{
					inschool: 0,
					outschool: 0,
				},
				selectInfoAccount: uni.getStorageSync('selectInfoAccount'),
				selectList:[
					'年级',
					'班级',
					'楼栋',
				],
				current:0,
				departList:[],
				timeRangeColumns: [
					[
						{
							label: '中午',
							value: 'noon',
						},
						{
							label: '晚上',
							value: 'night',
						},
					]
				],
				timeRangeShow :false,
				selecetDepart : {},
			}
		},
		components:{
		},
		methods: {
			departChangeHandler(e) {
			    const {
			        columnIndex,
			        value,
			        values, // values为当前变化列的数组内容
			        index,
					// 微信小程序无法将picker实例传出来，只能通过ref操作
			        picker = this.$refs.uPicker
			    } = e
			    // 当第一列值发生变化时，变化第二列(后一列)对应的选项
			    if (columnIndex === 0) {
			        // picker为选择器this实例，变化第二列对应的选项
			       picker.setColumnValues(1, value[index].children)
			    }else if(columnIndex==1){
					if(value[1].children&&value[1].children.length){
						picker.setColumnValues(2, value[1].children)
					}else{
						picker.setColumnValues(2, [])
					}
			    }
			},
			// 回调参数为包含columnIndex、value、values
			timeRangeConfirm(e) {
			    console.log('confirm', e)
				this.params.timeRange = e.value[0].value
				this.timeRangeStr = e.value[0].label
				this.timeRangeShow  = false
			},
			sectionChange(index){
				this.current = index
				this.getList()
			},
			handleDetail(item){
				this.goPage('/pages/componentsC/safe/accessRecordDetail',{id:1})
			},
			scrolltolower(){
				if(this.total == this.list.length){
					return this.setMessage('没有更多了')
				}
				this.params.pageNo ++
				this.getList()
				
			},
			goSearch(){
				this.params.pageNo = 1 
				this.getList()
			},
			attDateConfirm(e){
				let date = this.dateformat(e.value).split(' ')[0]
				this.params.attDate = date
				console.log(this.params.attDate);
				this.attDateShow = false
			},
			accessDate_endConfirm(e){
				let date = this.dateformat(e.value).split(' ')[0]
				this.params.accessDate_end = date
				this.accessDate_endShow = false
			},
			getDepart(){
				let params = {}
				let tempArr = this.selectInfoAccount.positionList
				let ids = []
				tempArr.forEach(item=>{
					ids.push(item.departmentId)
				})
				params.ids = ids.join(',')
				getDepartBypidOridApi(params).then(res=>{
					let tempArr = []
					// departList
					console.log(res.result);
					res.result.forEach(item=>{
						let tempIndex = tempArr.findIndex(item2=>{
							return item2.id==item.parentId
						})
						item.children = []
						console.log(tempIndex);
						if(tempIndex==-1){
							tempArr.push(item)
						}else{
							tempArr[tempIndex].children.push(item)
						}
					})
					
					this.departList = tempArr
					this.departColumns[0] = this.departList
					this.departList.forEach((item,index)=>{
						if(index==0){
							this.departColumns[1] = item.children
						}
						this.departColumnData.push(item.children)
					})
					console.log(this.departList);
				})
			},
			getList(){
				let params = this.params
				// params.accessType = this.current==0 ? 2 : 1
				// if(this.isTeacher==2) params.personId = this.selectInfoAccount.id
				// if(this.isTeacher==1){
				// 	let tempArr = this.selectInfoAccount.positionList
				// 	let obj = {
				// 		positionType: 0
				// 	}
				// 	tempArr.forEach(item=>{
				// 		if(item.positionType > obj.positionType) obj = item
				// 	})
				// 	params.personDepartmentId = obj.departmentId
				// 	params.personDepartmentId = this.selecetDepart.id ? this.selecetDepart.id : params.personDepartmentId
				// }
				params.groupType = this.current
				
				personStatisticsApi(params).then(res=>{
					// if(params.pageNo==1) this.list = []
					this.list = []
					this.list = this.list.concat(res.result)
					// this.total = res.result.total
				})
			},
			goSearch(){
				this.params.pageNo = 1
				this.getList()
			},
		},
		onLoad() {
			this.setPageTitle(this.language.bedtimeStatistics.title)
			this.params.attDate = this.dateformat(new Date()).split(' ')[0]
			 let now = new Date();
			  let currentHour = now.getHours();
			
			  if (currentHour >= 6 && currentHour < 18) {
			    // 白天
				this.params.timeRange = this.timeRangeColumns[0][0].value
				this.timeRangeStr = this.timeRangeColumns[0][0].label
			  } else {
			    // 夜晚
			    this.params.timeRange = this.timeRangeColumns[0][1].value
			    this.timeRangeStr = this.timeRangeColumns[0][1].label
			  }
			this.selectList[0] = this.language.bedtimeStatistics.type1
			this.selectList[1] = this.language.bedtimeStatistics.type2
			this.selectList[2] = this.language.bedtimeStatistics.type3
			this.getList()
			// this.getDepart()
		},
		onShow() {
			
		},
	}
</script>

<style lang="scss" scoped>
.page{
	background-color: #f1f1f1;
	// height: 100vh;
	.searchBox{
		display: flex;
		align-items: center;
		background-color: #fff;
		padding: 0 20rpx;
		border-bottom: 1px solid #f1f1f1;
		margin: 20rpx;
		border-radius: 8rpx;
		box-shadow: 0px 6rpx 12rpx rgba(0, 0, 0, 0.16);
		.inputBox{
			flex: 1;
			padding-right: 30rpx;
			.inputRow{
				height: 80rpx;
				display: flex;
				align-items: center;
				font-size: 32rpx;
				padding-left: 20rpx;
				.label{
					color: #191919;
					margin-right: 30rpx;
				}
			}
			.input{
				
			}
			.emty{
				color: #c0c4cc;
			}
			.val{
				
			}
		}
		.btnBox{
			display: flex;
			flex-direction: column;
			padding: 10rpx 0;
			.reset{
				margin-top: 20rpx;
				border: 1px solid #f1f1f1;
				background: #fff !important;
				color: #353535 !important;
			}
			.btn{
				padding: 0 30rpx;
				// width: 110rpx;
				height: 60rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				background: #1890ff;
				border-radius: 8rpx;
				color: #fff;
			}
		}
	}
	
	.boxItem{
			background-color: #fff;
			margin: 20rpx;
			border-radius: 8rpx;
			padding: 10rpx;
			box-shadow: 0px 0px 20rpx 0px rgba(102,102,102,0.15);
			.row{
				margin: 10rpx;
				display: flex;
				.item{
					width: 50%;
					// flex: 1;
				}
				.label{
					color: #969696;
				}
				.val{
					color: #353535;
					font-size: 32rpx;
				}
				.btn{
						padding: 0 30rpx;
						width: 120rpx;
						height: 60rpx;
						display: flex;
						align-items: center;
						justify-content: center;
						background: #1890ff;
						border-radius: 8rpx;
						color: #fff;
				}
			}
		}
		
		.totalRow{
			height: 120rpx;
			display: flex;
			background-color: #fff;
			box-shadow: 0px 6rpx 12rpx rgba(0, 0, 0, 0.16);
			margin: 10rpx 20rpx;
			border-radius: 8rpx;
			.item{
				display: flex;
				flex-direction: column;
				align-items: center;
				flex: 1;
				.label{
					font-weight: 600;
					font-size: 34rpx;
					margin: 10rpx;
				}
				.num{
					color: #4089f0;
					font-size: 32rpx;
				}
			}
		}
}
</style>
